package com.xvxingan.base.shiro;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.xvxingan.base.dao.BaseManager;
import com.xvxingan.utils.ValidUtil;

@Component
public class UserRealm extends AuthorizingRealm {
	@Autowired
	private HttpServletRequest request;

	private BaseManager baseManager;
	private ShiroFilterFactoryBean shiroFilterFactoryBean;
	
	public ShiroFilterFactoryBean getShiroFilterFactoryBean() {
		return shiroFilterFactoryBean;
	}
	@Resource
	public void setShiroFilterFactoryBean(
			ShiroFilterFactoryBean shiroFilterFactoryBean) {
		this.shiroFilterFactoryBean = shiroFilterFactoryBean;
	}
	public BaseManager getBaseManager() {
		return baseManager;
	}

	@Resource
	public void setBaseManager(BaseManager baseManager) {
		this.baseManager = baseManager;
	}

	@Override
	protected AuthorizationInfo doGetAuthorizationInfo(
			PrincipalCollection principals) {
/*		Subject currentUser = SecurityUtils.getSubject();
		User user = (User) currentUser.getSession().getAttribute("currentUser");		
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("userId",user.getId() );
		List<Role> roles = this.baseManager.query("Role.getUserAllRoles", parameters);
		SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
		for(Role role : roles){
			info.addRole(role.getId().toString());
		}
 		return info;*/
		return null;
	}

	@Override
	protected AuthenticationInfo doGetAuthenticationInfo(
			AuthenticationToken token) throws AuthenticationException {
		String phone = (String) token.getPrincipal();
		String passwd = new String((char[]) token.getCredentials());
/* 
		Map<String,Object> user = null;
 		 if(ValidUtil.validPhone(phone)){
			 user = this.userService.getByPhone(phone, passwd);
		 }else{
			 user = this.userService.getByUserName(phone, passwd);
		 }
		if(user==null){
			 throw new IncorrectCredentialsException(); 
		} 
		Subject currentUser = SecurityUtils.getSubject();
 		currentUser.getSession().setAttribute("currentUser", user);
		return new SimpleAuthenticationInfo(phone, passwd, "UserRealm");*/
		return null;
		
	}

	@Override
	public void clearCachedAuthorizationInfo(PrincipalCollection principals) {
		super.clearCachedAuthorizationInfo(principals);
	}

	@Override
	public void clearCachedAuthenticationInfo(PrincipalCollection principals) {
		super.clearCachedAuthenticationInfo(principals);
	}

	@Override
	public void clearCache(PrincipalCollection principals) {
		super.clearCache(principals);
	}

	public void clearAllCachedAuthorizationInfo() {
		getAuthorizationCache().clear();
	}

	public void clearAllCachedAuthenticationInfo() {
		getAuthenticationCache().clear();
	}

	public void clearAllCache() {
		clearAllCachedAuthenticationInfo();
		clearAllCachedAuthorizationInfo();
	}

}